/**
 * Created by xfz on 2015/5/24.
 */


var efficiency = function () {

    var saveValidate = function () {
        if ($("#e52State").is(':checked')) {
              var E52 = $("#E52").val();
              if (E52 == "") {
                  toast.error("机械未完全燃烧损失 请计算后再进行保存！")
                  return false;
              }
          }
        var E54 = $("#E54").val();
        if (E54 == "") {
            toast.error("外部冷却损失及未计入热损失 不能为空！")
            return false;
        }
        if ($("#e89State").is(':checked')) {
            var E89 = $("#E89").val();
            if (E89 == "") {
                toast.error("锅炉效率（低位） 不能为空！")
                return false;
            }
        }
        if ($("#e90State").is(':checked')) {
            var E90 = $("#E90").val();
            if (E90 == "") {
                toast.error("锅炉效率（高位） 不能为空！")
                return false;
            }
        }
        if ($("#e92State").is(':checked')) {
            var E92 = $("#E92").val();
            if (E92 == "") {
                toast.error("锅炉燃料量 不能为空！")
                return false;
            }
        }
        if ($("#e93State").is(':checked')) {
            var E93 = $("#E93").val();
            if (E93 == "") {
                toast.error("锅炉燃料量 不能为空！")
                return false;
            }
        }
        if ($("#e94State").is(':checked')) {
            var E94 = $("#E94").val();
            if (E94 == "") {
                toast.error("锅炉计算燃煤量 不能为空！")
                return false;
            }
        }
        if ($("#e95State").is(':checked')) {
            var E95 = $("#E95").val();
            if (E95 == "") {
                toast.error("锅炉计算燃煤量 不能为空！")
                return false;
            }
        }
        return true;
    }
    var getEfficiency = function () {
        var operateFiled =
        {
            e52State: $("#e52State").is(':checked') ? "1" : "0",
            e89State: $("#e89State").is(':checked') ? "1" : "0",
            e90State: $("#e90State").is(':checked') ? "1" : "0",
            e92State: $("#e92State").is(':checked') ? "1" : "0",
            e93State: $("#e93State").is(':checked') ? "1" : "0",
            e94State: $("#e94State").is(':checked') ? "1" : "0",
            e95State: $("#e95State").is(':checked') ? "1" : "0"
        }
        return{
            e52State: operateFiled.e52State,
            e89State: operateFiled.e89State,
            e90State: operateFiled.e90State,
            e92State: operateFiled.e92State,
            e93State: operateFiled.e93State,
            e94State: operateFiled.e94State,
            e95State: operateFiled.e95State,
            E52: operateFiled.e52State == '1' ? $("#E52").val() : 0,
            E51: $("#E51").val() == "" ? "0" : $("#E51").val(),
            E89: operateFiled.e89State == '1' ? $("#E89").val() : 0,
            E90: operateFiled.e90State == '1' ? $("#E90").val() : 0,
            E92: operateFiled.e92State == '1' ? $("#E92").val() : 0,
            E93: operateFiled.e93State == '1' ? $("#E93").val() : 0,
            E94: operateFiled.e94State == '1' ? $("#E94").val() : 0,
            E95: operateFiled.e95State == '1' ? $("#E95").val() : 0,
            E54: $("#E54").val(),
            powerCaseId: $("#caseId").val()
        }
    }

    return {
        init: function () {
            //E51弹出窗口关闭后，计算E52，并重新计算E89~E95
            $("#modal").on('hide.bs.modal', function () {
                var e51=$("#E51").val();
                $.ajax({
                    url: basePath + "boiler/efficiency/calculateTheValueOfE52",
                    dataType: "json",
                    async: "true",
                    type: "post",
                    data: "e51=" + e51,
                    success: function (data) {
                        $("input").autoInject({
                            data: data.returnData,
                            mode: "id"
                        })
                        initData = $.extend(true, {}, data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });
//            //修改E52数据，重新计算E89、E90、E93、E92、E94、E95并回填
//            $("#E52").on("blur.save", function () {
//                var $obj = $("#E52");
//                var E52 = $obj.val();
//                validate.init($obj);
//                if (validate.isNotNumber(E52) || E52 > 100) {
//                    validate.invalid();
//                    return;
//                }
//                validate.valid();
//                $.ajax({
//                    url: basePath + "boiler/efficiency/saveTheValueOfE52",
//                    dataType: "json",
//                    type: "post",
//                    data: "E52=" + E52,
//                    success: function (data) {
//                        $("input").autoInject({
//                            data: data.returnData,
//                            mode: "id"
//                        });
//                        initData = $.extend(true, {}, data.returnData);
//                        Metronic.unblockUI();
//                    }
//                })
//            });
            $("#calculateE51").click(function () {
                $.ajax({
                    url: basePath + "boiler/efficiency/calculateTheValueOfE51",
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        $("#E51").val(data.returnMsg);
                    }
                })
            });
            $("#e52State").bind("click", function () {
                efficiency.e52StateClick($(this))
            })
            $("#e89State").bind("click", function () {
                efficiency.e89StateClick($(this))
            })
            $("#e90State").bind("click", function () {
                efficiency.e90StateClick($(this))
            })
            $("#e92State").bind("click", function () {
                efficiency.e92StateClick($(this))
            })
            $("#e94State").bind("click", function () {
                efficiency.e94StateClick($(this))
            })
            $("#e93State").bind("click", function () {
                efficiency.e93StateClick($(this))
            })
            $("#e95State").bind("click", function () {
                efficiency.e95StateClick($(this))
            })
        },
        e52StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E52").attr("readonly", true)
                $("#calculateE51").show();
                $("#E52Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E52").val("")
                }
                $("#E52").attr("readonly", true);
                $("#calculateE51").hide();
                $("#E52Label").removeClass("aTypeStyle");


            }
        },
        e89StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E89").attr("readonly", false)
                $("#E89Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E89").val("")
                }
                $("#E89").attr("readonly", true);
                $("#E89Label").removeClass("aTypeStyle");
            }
        },
        e90StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E90").attr("readonly", false)
                $("#E90Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E90").val("")
                }
                $("#E90").attr("readonly", true);
                $("#E90Label").removeClass("aTypeStyle");
            }
        },
        e92StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E92").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#E92").val("")
                }
                $("#E92").attr("readonly", true);
            }
        },
        e94StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E94").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#E94").val("")
                }
                $("#E94").attr("readonly", true);
            }
        },
        e93StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E93").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#E93").val("")
                }
                $("#E93").attr("readonly", true);
            }
        },
        e95StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E95").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#E95").val("")
                }
                $("#E95").attr("readonly", true);
            }
        },
        efficiencyNext: function () {
            var addFlag = $("#addFlag").val();
            if (addFlag == "add") {
                toast.error("请先填写本页数据，保存成功后再进行下一页操作！");
                return;
            }
            formSub(basePath + "boiler/summary?menuId=21")

        },
        efficiencySave: function () {
            if (!saveValidate())return;
            var addFlag = $("#addFlag").val();
            if (addFlag == "add" || (addFlag != "add" && confirm("确定重新计算并保存吗？"))) {
                Metronic.blockUI({
                    boxed: true,
                    message: "数据加载中..."
                });
                $.ajax({
                    url: basePath + "boiler/efficiency/efficiencySave",
                    type: "post",
                    dataType: "json",
                    data: getEfficiency(),
                    success: function (data) {
                        Metronic.unblockUI();
                        if (data.ok) {
                            formSub(basePath + "boiler/efficiency?menuId=20&message=success")
                        } else if (data.error) {
                            toast.error(data.returnMsg);
                            return false;
                        }
                    }
                })
            }
        },
        recover: function () {
        }
    }
}();